<?php


/**
 * Description of Liability
 *
 * @author Admin
 */
class Dataform_Model_Liability extends WOW_Model_Acl_Abstract
{

    public function getResourceId()
    {
        return 'Liability';
    }

    public function getLiabilityDataById($id)
    {
        $id = (int) $id;
        return $this->getResource('Liability')->getLiabilityDataById($id);
    }

    public function getAllLiabilityData()
    {
         return $this->getResource('Liability')->getAllLiabilityData();
    }

    public function deleteLiabilityData($liability)
    {
        if ($liability instanceof Fataform_Resource_Liability_Item_Interface) {
            $liabilityid = (int) $liability->id;
        } else {
           $liabilityid = (int) $liability;
        }

        $liability = $this->getLiabilitytDataById($liabilityid);

        if (null !== $liability) {
            $liability->delete();
            return true;
        }
        else
             throw new WOW_Exception('Unknown Form field');

        return false;
    }

    public function saveLiabilityField($post)
    {
        $form = $this->getForm('LiabilityDataform');
        return $this->_save($form, $post);
    }

    protected function _save(Zend_Form $form, array $info, $defaults=array())
    {
        if (!$form->isValid($info))
       {
            // echo '<br> returning false formnot valid';
            return false;
       }
        $data = $form->getValues();

        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }

        if($data['id'] !='')
        {
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $liability = ($data['id'] !='') ? $this->getResource('Liability')->getLiabilityDataById($data['id']) : null;

        return $this->getResource('Liability')->saveRow($data, $liability);
    }

    #@ TODO:: needs to be worked on ACL later
    public function setAcl(WOW_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)->allow('Normal', $this, array('register'))
            ->allow('Moderator', $this, array('saveCategory'))
                    ->allow('Admin', $this);
        }
        $this->_acl = $acl;
        return $this;
    }

    #@ TODO:: needs to be worked on ACL later
    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->setAcl(new Main_Model_Acl_Main());
        }
        return $this->_acl;
    }

}